import requests
from bs4 import BeautifulSoup
import time

from 爬取网易云音乐 import download_music

url = 'https://music.163.com/discover/artist/cat?id=1001'

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}

response = requests.get(url, headers=headers)

if response.status_code == 200:
    # 想下载的歌手名字
    singer_names = ["汪苏泷"]
    # 想下载的歌曲名字
    # down_musics = None
    down_musics = ['为你写诗']
    soup = BeautifulSoup(response.text, 'html.parser')
    # singer_elements = soup.find_all(class_='msk')
    singer_elements = soup.find_all(class_='nm-icn')
    print('歌手数量:', len(singer_elements))
    for element in singer_elements:
        # 提取歌手IDs
        title = element['title']
        music_name = title.split('的')[0]
        # print(f'Singer name: {music_name}')
        href = element['href']
        music_id = href.split('=')[1]
        # print(f'Singer ID: {music_id}')
        if music_name in singer_names:
            print(f'Singer name: {music_name}')
            download_music(music_name, music_id, down_musics)
else:
    print(f"Failed to retrieve content with status code: {response.status_code}")

time.sleep(1)
